package com.ssfeng.youxia.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

public class SpringApplicationIntializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/*@Autowired
	private EnvironmentService environmentService;*/
	/***
	 * 家长 Spring 配置信息
	 * @return
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] {
				SpringRootConfig.class,
				SpringDatabaseConfig.class,
				CachingConfig.class,
				/*ShiroConfiguration.class,*/
				RedisConfig.class,
				BeanConfig.class,
				QuartzConfiguration.class
		};
	}

	/***
	 * 加载Spring mvc配置信息
	 * @return
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] { SpringWebConfig.class };
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

	/***
	 * 家长过滤器
	 * @return
	 */
	@Override
	protected Filter[] getServletFilters() {
		// 编码过滤器
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("utf-8");
		characterEncodingFilter.setForceEncoding(true);
		/*DelegatingFilterProxy shiroFilter = new DelegatingFilterProxy();
		shiroFilter.setTargetFilterLifecycle(true);
		shiroFilter.setTargetBeanName("shiroFilter");*/
		return new Filter[] { characterEncodingFilter/*, shiroFilter*/ };
	}

	/***
	 *文件上传设置
	 * @param registration
	 */
	@Override
	protected void customizeRegistration(ServletRegistration.Dynamic registration) {
		/***
		 * location 未绝对路径地址,缓存文件
		 */
//		String dir = environmentService.getProperty("file.spring.cache.dir");
		registration.setMultipartConfig(new MultipartConfigElement("/Users/chanming/Desktop/tmp", 2097152, 4194304, 0));
	}

}
